/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.model.usuario;

import br.com.bld.util.facade.AbstractDao;
import br.com.bld.util.facade.EntityFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;

/**
 *
 * @author Pedro
 */
class UsuarioDao extends AbstractDao {

    public UsuarioDao(Connection conn, EntityFactory entityFactory) {
        super(conn, entityFactory);
    }

    private UsuarioPK getPk(ResultSet rs) throws SQLException {

        int i = 0;

        UsuarioPK resposta = new UsuarioPK(rs.getLong(++i));

        return resposta;
    }

    private UsuarioEntity getEntity(ResultSet rs) throws SQLException {
        UsuarioEntity resposta = new UsuarioEntity();

        int i = 1;

        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setPrimeiroNome(rs.getString(i));
        }

        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setUltimoNome(rs.getString(i));
        }

        resposta.setLogin(rs.getString(++i));
        resposta.setSenha(rs.getString(++i));
        resposta.setEmail(rs.getString(++i));

        return resposta;
    }

    public int insert(UsuarioEntity entity, UsuarioPK pk) throws SQLException {

        String sql = createInsert();

        PreparedStatement ps = null;

        try {
            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            if (entity.getPrimeiroNome() != null && !entity.getPrimeiroNome().equals("")) {
                ps.setString(++i, entity.getPrimeiroNome());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getUltimoNome() != null && !entity.getUltimoNome().equals("")) {
                ps.setString(++i, entity.getUltimoNome());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            ps.setString(++i, entity.getLogin());
            ps.setString(++i, entity.getSenha());
            ps.setString(++i, entity.getEmail());

            return ps.executeUpdate();
        } finally {
            close(ps);
        }
    }

    public int update(UsuarioEntity entity, UsuarioPK pk) throws SQLException {

        String sql = createUpdate();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            if (entity.getPrimeiroNome() != null && !entity.getPrimeiroNome().equals("")) {
                ps.setString(++i, entity.getPrimeiroNome());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getUltimoNome() != null && !entity.getUltimoNome().equals("")) {
                ps.setString(++i, entity.getUltimoNome());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            ps.setString(++i, entity.getLogin());
            ps.setString(++i, entity.getSenha());
            ps.setString(++i, entity.getEmail());
            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public int delete(UsuarioPK pk) throws SQLException {
        String sql = createDelete();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public HashMap<UsuarioPK, UsuarioEntity> selectAll() throws SQLException {

        HashMap<UsuarioPK, UsuarioEntity> resposta = new HashMap();
        String sql = createSelect();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public UsuarioEntity selectByPk(UsuarioPK pk) throws SQLException {

        UsuarioEntity resposta = null;

        String sql = createSelectByPk();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public HashMap<UsuarioPK, UsuarioEntity> selectByPage(Integer numrPagina, Integer qtdePorPagina) throws SQLException {

        HashMap<UsuarioPK, UsuarioEntity> resposta = new HashMap();
        String sql = createSelectByPage(numrPagina, qtdePorPagina);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public UsuarioEntity selectByEmail(String email) throws SQLException {

        UsuarioEntity resposta = null;

        String[] key = {"email"};

        String sql = createSelectByKey(key);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = getPreparedStatement(sql);

            ps.setString(1, email);

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;

        } finally {
            close(ps, rs);
        }
    }
}
